<template>
  <div class="home">
    <!-- 头部搜索 -->
    <van-nav-bar class="navbar" fixed>
      <van-button slot="title"
      round
      icon="search"
      type="info"
      size="small"
      to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- 滚动导航条 -->
    <van-tabs class="tabs" v-model="active" animated swipeable>
      <!-- 遍历userChannels,动态生成频道 -->
      <van-tab :key="item.id" v-for="item in userChannels"
      :title="item.name">
      <!-- 对应频道的数据列表 -->
      <channels :channels="item"></channels>
      </van-tab>
      <div slot="nav-right" class="box"></div>
      <div slot="nav-right" class="hbg">
        <i class="toutiao toutiao-gengduo" @click="showPopup"></i>
      </div>
    </van-tabs>

    <!-- 编辑频道的弹出层 -->
    <van-popup
      v-model="popupShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
    <!-- 编辑模块内容的组件 -->
    <channelsEdit
    :userChannels="userChannels"
    :active="active"
    @updateActive="updateHomeActive"
    ></channelsEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user/user.js'
import channelsList from './tabList/channels.vue'
import channelsEdit from './tabList/channels_edit.vue'
import { mapState } from 'vuex'
export default {
  name: 'home',
  data () {
    return {
      active: 0,
      userChannels: [],
      popupShow: false
    }
  },
  // 引入频道列表组件
  components: {
    channels: channelsList,
    channelsEdit
  },
  created () {
    this.getUserChannel()
  },

  methods: {
    async getUserChannel () {
      // 获取用户频道
      // 判断用户是否登录，登录了就用用户自己的频道
      if (this.userTokens) {
        try {
          const { data } = await getUserChannels()
          this.userChannels = data.data.channels
          // console.log(data)
        } catch (err) {
          this.$toast.fail('获取频道失败了')
        }
      } else {
        // 未登录就用本地存储
        // 是否有本地频道，没有本地存储就发起请求获取头条默认的频道
        const localChannels = JSON.parse(localStorage.getItem('local_channels'))
        if (localChannels) {
          this.userChannels = localChannels
        } else {
          try {
            const { data } = await getUserChannels()
            this.userChannels = data.data.channels
          // console.log(data)
          } catch (err) {
            this.$toast.fail('获取频道失败了')
          }
        }
      }
    },
    // 点击编辑触发弹层
    showPopup () {
      this.popupShow = true
    },
    // 弹出层里面的我的频道，点击了我的频道元素就跳转到该页面
    updateHomeActive (index, flag = false) {
      this.active = index
      if (!flag) {
        this.popupShow = false
      }
    }
  },
  computed: {
    ...mapState(['userTokens'])
  }
}
</script>

<style scoped lang='less'>
.home {
  padding-top: 174px;
  padding-bottom: 100px;
}
.navbar {
  background-color: #3296fa!important;
  ::v-deep .van-nav-bar__title {
    max-width: unset!important;
  }
  .van-button {
  width: 555px;
  height: 64px;
  background-color: #5babfb;
  font-size: 28px;
  .van-icon {
    font-size: 32px;
  }
  }
}
::v-deep .tabs {
  .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 20;
      left: 0;
      right: 0;
  }
  .van-tabs__wrap {
    height: 82px!important;
  }
  .van-tab {
    min-width: 200px;
    border-right: 1px solid #edeff3;
  }
  .van-tab--active {
    color: #5babfb;
  }
  .van-tabs__line {
    bottom: 40px!important;
    width: 31px;
    height: 6px;
    background-color: #3296fa;
  }
  .van-tab__text {
    font-size: 28px;
  }
  .hbg {
    position: fixed;
    display: flex;
    justify-content:center;
    align-items:center;
    background-color: #fff;
    opacity: 0.9;
    right: 0;
    width: 66px;
    height: 82px;
    i.toutiao {
      font-size: 33px;
    }
  }
  .box {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
}
</style>
